package com.laozj.test.init;

import com.laozj.test.entity.UserEntity;
import com.laozj.test.service.UserService;
import com.laozj.test.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class InitRedisWmsInfo implements ApplicationRunner {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始初始化Redis数据...");
        new Thread(() -> {
            UserVO queryVO = new UserVO();
            queryVO.setId(1);
            List<UserEntity> userList = userService.getUserList(queryVO);
            if(!CollectionUtils.isEmpty(userList)){
                for (UserEntity entity : userList) {
                    redisTemplate.opsForValue().set(entity.getId().toString(), entity.getAge());
                }
            }
        }).start();
    }
}